﻿using System;
using System.IO;
using UnityEngine;

namespace SEXY
{
	// Token: 0x020002B3 RID: 691
	public static class PngAssist
	{
		// Token: 0x06001175 RID: 4469 RVA: 0x00071248 File Offset: 0x0006F448
		public static bool CheckPngData(Stream st, ref long size, bool skip)
		{
			if (st == null)
			{
				return false;
			}
			size = 0L;
			long position = st.Position;
			byte[] array = new byte[8];
			byte[] array2 = new byte[]
			{
				137,
				80,
				78,
				71,
				13,
				10,
				26,
				10
			};
			st.Read(array, 0, 8);
			for (int i = 0; i < 8; i++)
			{
				if (array[i] != array2[i])
				{
					st.Seek(position, SeekOrigin.Begin);
					return false;
				}
			}
			bool flag = true;
			while (flag)
			{
				byte[] array3 = new byte[4];
				st.Read(array3, 0, 4);
				Array.Reverse(array3);
				int num = BitConverter.ToInt32(array3, 0);
				byte[] array4 = new byte[4];
				st.Read(array4, 0, 4);
				int num2 = BitConverter.ToInt32(array4, 0);
				if (num2 == 1145980233)
				{
					flag = false;
				}
				st.Seek((long)(num + 4), SeekOrigin.Current);
			}
			size = st.Position - position;
			if (!skip)
			{
				st.Seek(position, SeekOrigin.Begin);
			}
			return true;
		}

		// Token: 0x06001176 RID: 4470 RVA: 0x00071340 File Offset: 0x0006F540
		public static bool CheckPngData(BinaryReader reader, ref long size, bool skip)
		{
			return reader != null && PngAssist.CheckPngData(reader.BaseStream, ref size, skip);
		}

		// Token: 0x06001177 RID: 4471 RVA: 0x00071358 File Offset: 0x0006F558
		public static bool CheckPngData(byte[] data, ref long size)
		{
			bool result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				memoryStream.Write(data, 0, data.Length);
				memoryStream.Seek(0L, SeekOrigin.Begin);
				result = PngAssist.CheckPngData(memoryStream, ref size, false);
			}
			return result;
		}

		// Token: 0x06001178 RID: 4472 RVA: 0x000713AC File Offset: 0x0006F5AC
		public static byte[] LoadPngData(string fullpath)
		{
			byte[] result;
			using (FileStream fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Read))
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					result = PngAssist.LoadPngData(binaryReader);
				}
			}
			return result;
		}

		// Token: 0x06001179 RID: 4473 RVA: 0x0007140C File Offset: 0x0006F60C
		public static byte[] LoadPngData(BinaryReader reader)
		{
			if (reader == null)
			{
				return null;
			}
			long num = 0L;
			PngAssist.CheckPngData(reader.BaseStream, ref num, false);
			if (num == 0L)
			{
				return null;
			}
			return reader.ReadBytes((int)num);
		}

		// Token: 0x0600117A RID: 4474 RVA: 0x00071448 File Offset: 0x0006F648
		public static Sprite LoadSpriteFromFile(string path, int width, int height, Vector2 pivot)
		{
			if (!File.Exists(path))
			{
				GlobalData.Debug_LogError(path + " 不存在");
				return null;
			}
			Sprite result;
			using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					byte[] data = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
					Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);
					if (null == texture2D)
					{
						result = null;
					}
					else
					{
						texture2D.LoadImage(data);
						if (width == 0 || height == 0)
						{
							width = texture2D.width;
							height = texture2D.height;
						}
						result = Sprite.Create(texture2D, new Rect(0f, 0f, (float)width, (float)height), pivot, 100f, 0U, SpriteMeshType.FullRect);
					}
				}
			}
			return result;
		}

		// Token: 0x0600117B RID: 4475 RVA: 0x00071538 File Offset: 0x0006F738
		public static Texture2D ChangeTextureFromByte(byte[] data, int width = 0, int height = 0)
		{
			Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);
			if (null == texture2D)
			{
				return null;
			}
			texture2D.LoadImage(data);
			return texture2D;
		}

		// Token: 0x0600117C RID: 4476 RVA: 0x00071568 File Offset: 0x0006F768
		public static Sprite LoadSpriteFromFile(string path)
		{
			Sprite result;
			using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
			{
				long num = 0L;
				PngAssist.CheckPngData(fileStream, ref num, false);
				if (num == 0L)
				{
					result = null;
				}
				else
				{
					using (BinaryReader binaryReader = new BinaryReader(fileStream))
					{
						byte[] data = binaryReader.ReadBytes((int)num);
						int num2 = 0;
						int num3 = 0;
						Texture2D texture2D = PngAssist.ChangeTextureFromPngByte(data, ref num2, ref num3);
						if (null == texture2D)
						{
							result = null;
						}
						else
						{
							result = Sprite.Create(texture2D, new Rect(0f, 0f, (float)num2, (float)num3), new Vector2(0.5f, 0.5f), 100f, 0U, SpriteMeshType.FullRect);
						}
					}
				}
			}
			return result;
		}

		// Token: 0x0600117D RID: 4477 RVA: 0x00071644 File Offset: 0x0006F844
		public static Texture2D ChangeTextureFromPngByte(byte[] data, ref int width, ref int height)
		{
			Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);
			if (null == texture2D)
			{
				return null;
			}
			texture2D.LoadImage(data);
			width = texture2D.width;
			height = texture2D.height;
			return texture2D;
		}

		// Token: 0x0600117E RID: 4478 RVA: 0x00071684 File Offset: 0x0006F884
		public static Texture2D LoadTexture(string _path)
		{
			Texture2D result;
			using (FileStream fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read))
			{
				long num = 0L;
				PngAssist.CheckPngData(fileStream, ref num, false);
				if (num == 0L)
				{
					result = null;
				}
				else
				{
					using (BinaryReader binaryReader = new BinaryReader(fileStream))
					{
						byte[] data = binaryReader.ReadBytes((int)num);
						int num2 = 0;
						int num3 = 0;
						result = PngAssist.ChangeTextureFromPngByte(data, ref num2, ref num3);
					}
				}
			}
			return result;
		}
	}
}
